<?php
class Page {
	protected static $first_page;	//第一頁
	protected static $last_page;	//最後一頁
	protected static $curr_page;	//當前頁  *param*
	protected static $pre_page;		//上一頁
	protected static $next_page;	//下一頁
	protected static $data_total;	//總數據數 *param*
	protected static $page_size;	//每頁顯示數 *param*
	protected static $page_count;	//總頁數
	protected static $url;			//分頁連接 *param*
	protected static $delimiter;	//加分頁參數的符號，'?' or '&'
	
	/**
	 * 配置參數
	 * @param $param array('page','total','size','url','num')
	 */
	static public function set($param) {
		self::$data_total = isset($param['total'])&&intval($param['total'])>0?intval($param['total']):0;
		self::$page_size = isset($param['size'])&&intval($param['size'])>1?intval($param['size']):1;
		self::$page_count = @ceil(self::$data_total/self::$page_size);
		self::$curr_page = isset($param['page'])&&intval($param['page'])>1?((intval($param['page'])>self::$page_count)?self::$page_count:intval($param['page'])):1;
		self::$pre_page = (self::$curr_page>1)?(self::$curr_page-1):1;
		self::$next_page = (self::$curr_page<self::$page_count)?(self::$curr_page+1):self::$page_count;
		self::$first_page = 1;
		self::$last_page = self::$page_count;
		self::$url = isset($param['url'])?$param['url']:'/';
		self::$delimiter = strstr(self::$url,"?") ? '&' : '?';
	}
	
	/**
	 * 分頁顯示輸出html
	 * @param $param_page	當為數組時，後面參數無效，直接set()配置此數組， 否則為當前頁
	 * @param $total		總數據數
	 * @param $size			每頁顯示數
	 * @param $url			分頁link url
	 */
	static public function show($param_page = 1,$total=0,$size=1,$url='/') {
		//  |< [1] ... [3] [4] 5 [6] [7] ... [9] >|
		//  |< [1] [2] [3] 4 [5] [6] ... [9] >|
		//  |< [1] ... [6] [7] 8 [9] >|
		//  |< 1 [2] [3] ... [9] >|
		//  |< [1] ... [7] [8] 9 >|
		if(is_array($param_page)){
			self::set($param_page);
		}else{
			self::set(array('page'=>$param_page,'total'=>$total,'size'=>$size,'url'=>$url));
		}
		$html = '';
		if(self::$page_count>1){
			$pre_h = (self::$curr_page>1) ? '<a class="pre_page" href="' . self::$url .self::$delimiter.'page='.self::$pre_page.'">上一頁 </a>':'<span class="pre_page_unable">上一頁</span>';
			$first_h = (self::$curr_page>1) ? '<a class="num_page" href="' . self::$url .self::$delimiter.'page='.self::$first_page.'">'.self::$first_page.'</a>':'';
			
			$last_h = (self::$curr_page<self::$page_count) ? '<a class="num_page" href="' . self::$url .self::$delimiter.'page='.self::$last_page.'">'.self::$last_page.'</a>':'';
			$next_h = (self::$curr_page<self::$page_count) ? '<a class="next_page" href="' . self::$url .self::$delimiter.'page='.self::$next_page.'">下一頁 </a>':'<span class="next_page_unable">下一頁</span>';
			$left_ellipsis = (self::$curr_page-self::$first_page-2>1) ? '<span class="text">...</span>' : '';
			$right_ellipsis = (self::$page_count-self::$curr_page-2>1) ? '<span class="text">...</span>' : '';
			
			$curr_list_h = '<span class="curr_page">'.self::$curr_page.'</span>';
			$left_list_h = $right_list_h = '';
			for ($i = 2; $i > 0; $i--) {
				$list_num = self::$curr_page-$i;
				$left_list_h .= $list_num>self::$first_page ? '<a class="num_page" href="'. self::$url .self::$delimiter.'page='.$list_num.'">'.$list_num.'</a>' : '';
			}
			for ($i = 1; $i <= 2; $i++) {
				$list_num = self::$curr_page+$i;
				$right_list_h .= $list_num<self::$page_count ? '<a class="num_page" href="'. self::$url .self::$delimiter.'page='.$list_num.'">'.$list_num.'</a>' : '';
			}
			
			$html = '<div class="pagelist">'.$pre_h.$first_h.$left_ellipsis.$left_list_h.$curr_list_h.$right_list_h.$right_ellipsis.$last_h.$next_h.'</div>';
		}
		return $html;
	}
}
?>